package hyit.app.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import hyit.app.dao.IGroupInfoDAO;
import hyit.app.model.GroupInfo;

public class GroupInfoDAOImpl implements IGroupInfoDAO {
	private Connection conn = null;
	private PreparedStatement pstmt = null;

	public GroupInfoDAOImpl(Connection conn) {
		this.conn = conn;
	}

	@Override
	public boolean doCreate(GroupInfo groupInfo) throws Exception {
		boolean flag = false;
		String sql = "INSERT INTO group_info VALUES(NULL,?,?)";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setInt(1, groupInfo.getGid());
		this.pstmt.setLong(2, groupInfo.getMemberId());
		if (this.pstmt.executeUpdate() > 0) {
			flag = true;
		}
		this.pstmt.close();
		return flag;
	}

	@Override
	public boolean update(GroupInfo groupInfo) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean delete(Integer number) throws Exception {
		boolean flag = false;
		String sql = "DELETE FROM group_info WHERE group_info.id = ?";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setInt(1, number);
		if (this.pstmt.executeUpdate() > 0) {
			flag = true;
		}
		this.pstmt.close();
		return flag;
	}

	@Override
	public List<GroupInfo> findByGid(Integer number) throws Exception {
		List<GroupInfo> all = new ArrayList<GroupInfo>();
		GroupInfo groupInfo = null;
		String sql = "SELECT id,gid,memberid FROM group_info WHERE gid = ?";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setInt(1, number);
		ResultSet rs = this.pstmt.executeQuery();
		while (rs.next()) {
			groupInfo = new GroupInfo();
			groupInfo.setId(rs.getInt("id"));
			groupInfo.setGid(rs.getInt("gid"));
			groupInfo.setMemberId(rs.getLong("memberid"));
			all.add(groupInfo);
		}
		this.pstmt.close();
		return all;
	}

	@Override
	public List<GroupInfo> findByMemberId(Long number) throws Exception {
		List<GroupInfo> all = new ArrayList<GroupInfo>();
		GroupInfo groupInfo = null;
		String sql = "SELECT id,gid,memberid FROM group_info WHERE memberid = ?";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setLong(1, number);
		ResultSet rs = this.pstmt.executeQuery();
		while (rs.next()) {
			groupInfo = new GroupInfo();
			groupInfo.setId(rs.getInt("id"));
			groupInfo.setGid(rs.getInt("gid"));
			groupInfo.setMemberId(rs.getLong("memberid"));
			all.add(groupInfo);
		}
		this.pstmt.close();
		return all;
	}

	@Override
	public GroupInfo findByGidAndMemberId(Integer gid, Long memberId)
			throws Exception {
		GroupInfo groupInfo = null;
		String sql = "SELECT id,gid,memberid FROM group_info WHERE gid = ? AND memberid = ?";
		this.pstmt = this.conn.prepareStatement(sql);
		this.pstmt.setInt(1, gid);
		this.pstmt.setLong(2, memberId);
		ResultSet rs = this.pstmt.executeQuery();
		while (rs.next()) {
			groupInfo = new GroupInfo();
			groupInfo.setId(rs.getInt("id"));
			groupInfo.setGid(rs.getInt("gid"));
			groupInfo.setMemberId(rs.getLong("memberid"));
		}
		this.pstmt.close();
		return groupInfo;
	}

}
